//
//  ScreenRulerExtensibleRulerView.m
//  ScreenRuler
//
//  Created by Tianzhou on 08-12-23.
//  Copyright 2008 Tianzhou Software Inc.. All rights reserved.

//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.

#import "ScreenRulerExtensibleRulerView.h"


@interface ScreenRulerExtensibleRulerView(private_method)

- (NSRect)resizeKnobRect;

@end

@implementation ScreenRulerExtensibleRulerView(private_method)

- (NSRect)resizeKnobRect
{
	if ([self orientation] == NSVerticalRuler)
	{
		return NSMakeRect([self bounds].size.width - [self ruleThickness]
						  ,[self bounds].size.height - kScreenRulerResizeKnobEdgeLength
						  ,[self ruleThickness]
						  ,kScreenRulerResizeKnobEdgeLength);	
	}
	else
	{
		return NSMakeRect([self bounds].size.width - kScreenRulerResizeKnobEdgeLength
						  ,[self bounds].size.height - [self ruleThickness]
						  ,kScreenRulerResizeKnobEdgeLength
						  ,[self ruleThickness]);
	}

}

@end


@implementation ScreenRulerExtensibleRulerView

- (id)init
{
	if ((self = [super init]) != nil)
	{
		isResizing_ = NO;
	}
	return self;
}

- (void)mouseDragged:(NSEvent *)theEvent
{
	if (isResizing_)
	{
		NSPoint thePoint = [theEvent locationInWindow];
		NSRect theFrame = [[enclosingScrollView_ window] frame];
		
		if ([theEvent window] != nil)
		{
			thePoint = [[self window] convertBaseToScreen:thePoint];
		}
		
		
		if ([self orientation] == NSVerticalRuler)
		{
#if USE_SPECIALCURSOR_FOR_DRAGGING
			[[NSCursor resizeUpCursor] set];
#endif
			NSUserDefaultsController *defaults = [NSUserDefaultsController sharedUserDefaultsController];
			id values = [defaults values];
			NSNumber *horzRulerLocation = [values valueForKey:kScreenRulerHorizontalRulerLocationKey];
			if ([horzRulerLocation intValue] == kTop)
			{
				if (resizeStartPointForTopHorzRuler_.y - thePoint.y > kMinRulerLength)
				{
					[[enclosingScrollView_ window] setFrame:NSMakeRect(theFrame.origin.x, thePoint.y - resizeStartPoint_.y, theFrame.size.width, resizeStartPointForTopHorzRuler_.y - thePoint.y)
													display:YES];
				}
			}
			else
			{
				if (thePoint.y - resizeStartPoint_.y > kMinRulerLength)
				{
					[[enclosingScrollView_ window] setFrame:NSMakeRect(theFrame.origin.x, theFrame.origin.y, theFrame.size.width, thePoint.y - resizeStartPoint_.y)
													display:YES];
				}
			}
		}
		else
		{
#if USE_SPECIALCURSOR_FOR_DRAGGING
			[[NSCursor resizeRightCursor] set];
#endif
			if (thePoint.x - resizeStartPoint_.x > kMinRulerLength)
			{
				[[enclosingScrollView_ window] setFrame:NSMakeRect(theFrame.origin.x, theFrame.origin.y, thePoint.x - resizeStartPoint_.x, theFrame.size.height)
												display:YES];
			}
		}
	}
	else
	{
		[super mouseDragged:theEvent];
	}
}

- (void)mouseDown:(NSEvent *)theEvent
{
	NSPoint thePoint1 = [theEvent locationInWindow];
	
	if ([theEvent window] == nil)
	{
		thePoint1 = [[self window] convertScreenToBase:thePoint1];
	}
	thePoint1 = [self convertPoint:thePoint1 fromView:nil];
	
	if (NSPointInRect(thePoint1,[self resizeKnobRect]))
	{
		isResizing_ = YES;
		thePoint1 = [theEvent locationInWindow];
		if ([theEvent window] != nil)
		{
			thePoint1 = [[self window] convertBaseToScreen:thePoint1];
		}
		
		thePoint1.x = thePoint1.x - [[enclosingScrollView_ window] frame].size.width;
			
		NSPoint thePoint2 = thePoint1;
		NSUserDefaultsController *defaults = [NSUserDefaultsController sharedUserDefaultsController];
		id values = [defaults values];
		NSNumber *horzRulerLocation = [values valueForKey:kScreenRulerHorizontalRulerLocationKey];
		if ([horzRulerLocation intValue] == kTop)
		{
			thePoint1.y = thePoint1.y - [[enclosingScrollView_ window] frame].origin.y;
			thePoint2.y = thePoint2.y + [[enclosingScrollView_ window] frame].size.height;
		}
		else
		{
			thePoint1.y = thePoint1.y - [[enclosingScrollView_ window] frame].size.height;
		}
			
		resizeStartPoint_ = thePoint1;
		resizeStartPointForTopHorzRuler_ = thePoint2;
	}
	else
	{
		[[self window] mouseDown:theEvent];
	}
}

- (void)mouseUp:(NSEvent *)theEvent
{
	isResizing_ = NO;
}

- (void)drawRect:(NSRect)theRect
{
	[super drawRect:theRect];
	[[NSColor blueColor] set];
	NSRectFill([self resizeKnobRect]);
}

#if USE_SPECIALCURSOR_FOR_DRAGGING
- (void)mouseMoveReceived:(NSEvent *)theEvent
{
	NSPoint thePoint = [theEvent locationInWindow];
	
	if ([theEvent window] == nil)
	{
		thePoint = [[self window] convertScreenToBase:thePoint];
	}
	thePoint = [self convertPoint:thePoint fromView:nil];
	
	if (NSPointInRect(thePoint,[self resizeKnobRect]))
	{
		if ([self orientation] == NSVerticalRuler)
		{
			[[NSCursor resizeUpCursor] set];
		}
		else
		{
			[[NSCursor resizeRightCursor] set];
		}
	}
	else
	{
		[[NSCursor arrowCursor] set];
	}
}
#endif

- (void)setEnclosingScrollerView:(NSScrollView *)theScrollView
{
	enclosingScrollView_ = theScrollView;
}

@end
